# GPPPARAMS = -m32 -fno-use-cxa-atexit -fleading-underscore -fno-exceptions -fno-builtin -nostdlib -fno-rtti -fno-pie

GPPPARAMS = -m32 -fno-builtin -fno-PIC -Wall -ggdb -gstabs -nostdlib -nostdinc -fno-stack-protector -Os 
ASPRAMS = --32
LDPARAMS = -melf_i386 -no-pie

objects = obj/boot/loader.o			\
		  obj/boot/kern.o 			\
		  obj/inc/stdio.o			\
		  obj/inc/vcprintf.o		\
		  obj/gdt/gdt.o				\
		  obj/pic/pic.o				\
		  obj/trap/trap.o			\
		  obj/trap/trapentry.o		\
		  obj/trap/vectors.o		\
		  obj/driver/intr.o			\
		  obj/driver/clock.o		\
		  obj/driver/console.o		\
		  obj/libs/string.o			\
		  obj/debug/kdebug.o		\
		#   obj/debug/panic.o			\
		#   obj/libs/readline.o		\
		#   obj/mm/pmm.o				\
		#   obj/mm/default_pmm.o		\

obj/%.o: src/%.c
	mkdir -p $(@D)
	gcc ${GPPPARAMS} -o $@ -c $<

obj/%.o : src/%.S
	mkdir -p $(@D)
	as ${ASPRAMS} -o $@ $<

kern.bin: linked.ld ${objects}
	ld ${LDPARAMS} -T $< -o $@ ${objects}
	cp kern.bin obj/
	rm -rf kern.bin

install: kern.bin
	cp $< /boot/kern.bin

kern.iso: kern.bin
	mkdir iso
	mkdir iso/boot
	mkdir iso/boot/grub
	cp obj/$< iso/boot
	echo 'set timeout = 0' > iso/boot/grub/grub.cfg
	echo 'set default = 0' >> iso/boot/grub/grub.cfg
	echo '' >> iso/boot/grub/grub.cfg
	echo 'menuentry "my os" {' >> iso/boot/grub/grub.cfg
	echo '	multiboot /boot/kern.bin' >> iso/boot/grub/grub.cfg
	echo '	boot' >> iso/boot/grub/grub.cfg
	echo '}' >> iso/boot/grub/grub.cfg
	grub-mkrescue --output=$@ iso
	rm -rf iso
	cp kern.iso obj/
	rm -rf kern.iso
run: kern.iso
	(killall virtualboxvm && sleep 1) || true
	virtualboxvm --startvm "kern" &

.PHONY: clean
clean: 
	rm -rf ${objects} obj/kern.iso obj/kern.bin

#sudo apt-get install gcc-multilib g++-mulutilib libc6-dev-i386